package my.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import bean.ConnectionBean;

public class CopyOfConnectionPool {
	// 最大连接池的容量
	private static final int MAX_CONNECTION_POOL_SIZE = 10;

	// 初始化连接池的容量大小
	private static final int INIT_CONNECTION_POOL_SIZE = 1;

	// 键代表连接池的类型
	private static final Map<String, Vector<ConnectionBean>> connectionPoolMap = new HashMap<String, Vector<ConnectionBean>>();

	public static final int MYSQL_DB = 0;
	public static final int SQLSERVER_DB = 1;

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 创建一个数据库连接
	private static Connection createConnection(String IP, String userName, String passWord, String DbName, int dbType) {
		Connection con = null;
		try {
			if (dbType == CopyOfConnectionPool.MYSQL_DB) {
				con = DriverManager.getConnection("jdbc:mysql://" + IP.trim() + ":8999/" + DbName.trim() + "?user=" + userName.trim() + "&password=" + passWord
						+ "&useUnicode=true&characterEncoding=UTF8");
			} else if (dbType == CopyOfConnectionPool.SQLSERVER_DB) {
				con = DriverManager.getConnection("jdbc:sqlserver://" + IP.trim() + ":1433;DatabaseName=" + DbName.trim() + "", "" + userName.trim() + "", "" + passWord.trim()
						+ "");
			} else {
				con = null;
			}
			System.out.println("init connection: " + con);
		} catch (Exception e) {
			con = null;
			e.printStackTrace();
		}
		return con;
	}

	// 创建一个连接池
	private static void createConnectionPool(int initPoolConnectionSize, String IP, String userName, String passWord, String DbName, int dbType, String typeName) {
		System.out.println("begin init connectionPool....." + typeName);
		Vector<ConnectionBean> connectionPool = new Vector<ConnectionBean>();
		for (int i = 0; i < initPoolConnectionSize; i++) {
			ConnectionBean conBean = new ConnectionBean(createConnection(IP, userName, passWord, DbName, dbType));
			connectionPool.add(conBean);
		}
		connectionPoolMap.put(typeName, connectionPool);
		System.out.println("init connectionPool.....end..." + typeName);
	}

	// 得到一个空闲的连接
	public static synchronized ConnectionBean getFreeConnection(String IP, String userName, String passWord, String DbName, int dbType, String typeName) {
		Vector<ConnectionBean> connectionPool = connectionPoolMap.get(typeName);

		if (connectionPool == null || connectionPool.size() == 0) {
			createConnectionPool(INIT_CONNECTION_POOL_SIZE, IP, userName, passWord, DbName, dbType, typeName);
			connectionPool = connectionPoolMap.get(typeName);
		}

		for (int i = 0; i < connectionPool.size(); i++) {
			ConnectionBean conbean = connectionPool.get(i);
			if (!conbean.isBusy()) {
				conbean.setBusy(true);
				return conbean;
			}
		}

		if (connectionPool.size() < MAX_CONNECTION_POOL_SIZE) {
			ConnectionBean conBean = new ConnectionBean(createConnection(IP, userName, passWord, DbName, dbType), true);
			connectionPool.add(conBean);
			connectionPoolMap.put(typeName, connectionPool);
			return conBean;
		}

		getConnectionForWait(100);
		return getFreeConnection(IP, userName, passWord, DbName, dbType, typeName);
	}

	private static void getConnectionForWait(int millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static synchronized void closeConnection(Connection con, String typeName) {
		Vector<ConnectionBean> connectionPool = connectionPoolMap.get(typeName);
		for (int i = 0; i < connectionPool.size(); i++) {
			ConnectionBean conbean = connectionPool.get(i);
			if (conbean.getCon() == con) {
				conbean.setBusy(false);
			}
		}
	}

	public static synchronized void clearAllConnection(String typeName) {
		Vector<ConnectionBean> connectionPool = connectionPoolMap.get(typeName);
		try {
			for (ConnectionBean connectionBean : connectionPool) {
				Connection con = connectionBean.getCon();
				if (con != null) {
					con.close();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		connectionPool.removeAllElements();
		System.out.println("already clear all connetions...." + typeName);
	}

	public static void main(String[] args) {
		Connection con1 = CopyOfConnectionPool.getFreeConnection("10.190.70.181", "root", "gsmdb112", "gsmapp", CopyOfConnectionPool.MYSQL_DB, "bf181").getCon();
		System.out.println(con1);
		CopyOfConnectionPool.closeConnection(con1, "bf181");
		Connection con3 = CopyOfConnectionPool.getFreeConnection("10.190.70.181", "root", "gsmdb112", "gsmapp", CopyOfConnectionPool.MYSQL_DB, "bf181").getCon();
		System.out.println(con3 + "====");
		Connection con2 = CopyOfConnectionPool.getFreeConnection("10.190.75.146", "root", "gsmdb112", "gsmapp", CopyOfConnectionPool.MYSQL_DB, "bf146").getCon();
		System.out.println(con2);
		CopyOfConnectionPool.closeConnection(con2, "bf146");
		Connection con4 = CopyOfConnectionPool.getFreeConnection("10.190.75.146", "root", "gsmdb112", "gsmapp", CopyOfConnectionPool.MYSQL_DB, "bf146").getCon();
		System.out.println(con4 + "====");
	}
}
